package com.trxy.hotelsystem.contorller;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.trxy.hotelsystem.model.clientInf;
import com.trxy.hotelsystem.model.floor;
import com.trxy.hotelsystem.model.houseType;
import com.trxy.hotelsystem.model.kaifanRecord;
import com.trxy.hotelsystem.model.room;
import com.trxy.hotelsystem.model.staffInf;
import com.trxy.hotelsystem.model.vip_management;
import com.trxy.hotelsystem.service.impl.Consumption_recordServiceImpl;
import com.trxy.hotelsystem.service.impl.FloorserviceImpl;
import com.trxy.hotelsystem.service.impl.KaifanRecordServiceImpl;
import com.trxy.hotelsystem.service.impl.RoomserviceImpl;
import com.trxy.hotelsystem.service.impl.RoomtypeServiceImpl;
import com.trxy.hotelsystem.service.impl.VipServiceImpl;
import com.trxy.hotelsystem.service.impl.Vip_managementServiceImpl;

import frame.uitls.PrimaryKeyUtil;
import net.sf.json.JSONArray;

@Controller
public class CheckinController {
	
	@Autowired
	private VipServiceImpl VipserviceImpl;
	
	@Autowired
	private FloorserviceImpl floorserviceImpl;
	
	@Autowired
	private RoomtypeServiceImpl roomtypeServiceImpl;
	
	@Autowired
	private RoomserviceImpl roomserviceImpl;
	
	@Autowired
	private KaifanRecordServiceImpl KaifanRecordserviceImpl;
	
	@Autowired
	private Consumption_recordServiceImpl consumption_recordServiceImpl;
	
	@Autowired
	private Vip_managementServiceImpl vip_managementServiceImpl;
	
	
	@RequestMapping(value = "/vipCheckin.do")
	public void vipCheckin(HttpServletRequest request,HttpServletResponse response,Model model,
			clientInf client,room Room) throws IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		String number = request.getParameter("number");
		
		List<clientInf> clientList = VipserviceImpl.selectVipBynumber(number);
		
		String st = JSONArray.fromObject(clientList).toString();
		response.getWriter().write(st);
		
		
	}
	
	//查询楼层
	@RequestMapping(value = "queryFloorAll.do")
	public void queryFloorAll(HttpServletRequest request,HttpServletResponse response) throws IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		List<floor> list = floorserviceImpl.queryFloorAll();
		
		String st = JSONArray.fromObject(list).toString();
		response.getWriter().write(st);
		
	}
	
	
	//查询楼层
	@RequestMapping(value = "queryHousetype.do")
	public void queryHousetype(HttpServletRequest request,HttpServletResponse response) throws IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		List<houseType> list = roomtypeServiceImpl.queryHouseAll();
		
		String st = JSONArray.fromObject(list).toString();
		response.getWriter().write(st);
		
	}
	
	
	//查询楼层
	@RequestMapping(value = "selectRoom.do")
	public void selectRoom(HttpServletRequest request,HttpServletResponse response,
			room rom) throws IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		String floor = request.getParameter("floor");
		String housetype = request.getParameter("housetype");
		
		String floor2 = floor.trim();
		String housetype2 = housetype.trim();
		
		if(floor2.equals("默认")) {
			rom.setFloor(null);
		}else {
			rom.setFloor(floor2);
		}
		if(housetype2.equals("默认")) {
			rom.setType(null);
		}else {
			rom.setType(housetype2);
		}
		
		rom.setStatus("空闲");
		
		
		List<room> list = roomserviceImpl.selectRoomByother(rom);
		
		String st = JSONArray.fromObject(list).toString();
		response.getWriter().write(st);
		
	}
	
	//开房记录
	
	@RequestMapping(value = "/checkIn.do")
	public String checkIn(HttpServletRequest request,HttpServletResponse response,
			kaifanRecord kaifanrecord,HttpSession session,clientInf clientinf,room room,Model model,
			vip_management vipmanagement,clientInf client) {
		
		//获取参数
		String vipNumber = request.getParameter("vipNumber");
		String roomNumber = request.getParameter("roomNumber");
		String people = request.getParameter("people");
		String remarks = request.getParameter("remarks");
		
		//获取session
		staffInf us = (staffInf) session.getAttribute("loginUser");
		
		double money = 0;
		//通过账号查询会员
		List<clientInf> clientin = VipserviceImpl.selectVipBynumber(vipNumber);
		
		//通过房间号查询房间
		room rom = roomserviceImpl.selectRoomByNumber2(roomNumber);
		String orderNumber = PrimaryKeyUtil.getPrimaryKey();
		//遍历会员信息
		String grade2 = null;
		for(clientInf one:clientin) {
			kaifanrecord.setId(orderNumber);
			kaifanrecord.setNumber(one.getNumber());
			kaifanrecord.setName(one.getName());
			kaifanrecord.setType(rom.getType());
			
			kaifanrecord.setQuantity(Integer.parseInt(people));
			kaifanrecord.setIdnumber(one.getIdnumber());
			kaifanrecord.setAddress(one.getAddress());
			kaifanrecord.setStatime(new Date());
			kaifanrecord.setTransactor(us.getName());
			kaifanrecord.setRemarks(remarks);
			kaifanrecord.setRoomnumber(Integer.parseInt(roomNumber));
			
			money = one.getMoney();
			grade2 = one.getGrade();
		}
		
		//获取会员折扣
		vip_management vaimanag = vip_managementServiceImpl.selectBygrade(grade2);
		double discount = vaimanag.getDiscounts();
		
		if(discount == 0) {
			kaifanrecord.setPrice(rom.getPrice());
		}else {
			kaifanrecord.setPrice(rom.getPrice()*discount);
		}
		
		if(money >= rom.getPrice()) {
			
			
			//插入开房记录
			int i = KaifanRecordserviceImpl.insertSelective(kaifanrecord);
			
			if(i > 0) {
				
				clientinf.setNumber(vipNumber);
				if(discount != 0) {
					clientinf.setMoney(money - rom.getPrice()*discount);
				}else {
					clientinf.setMoney(money - rom.getPrice());
				}
				
				//更新会员余额
				int j = VipserviceImpl.updateByPrimaryKeySelective(clientinf);
				
				//更新房间状态
				room.setReserved1(orderNumber);
				room.setStatus("使用中");
				room.setReserved2(vipNumber);
				room.setRoomnum(Integer.parseInt(roomNumber));
				room.setReserved3(people);
				int k = roomserviceImpl.updeRoomByroomnumber(room);
				
				model.addAttribute("title", "入住办理成功");
				model.addAttribute("content1", "房间号："+roomNumber);
				model.addAttribute("content2", "房价："+rom.getPrice()+"￥");
				model.addAttribute("content3", "入住人数："+people);
				return "view/addStafSuccess";
			}else {
				return "view/addStfdefeated";
			}
			
			
		}else {
			return "view/addStfdefeated";
		}	
	}
	
	//查看会员详细信息
	@RequestMapping(value = "vipDetailedInf.do")
	public void vipDetailedInf(HttpServletRequest request,HttpServletResponse response,
			clientInf clientinf) throws IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		String id = request.getParameter("id");

		List<clientInf> list = VipserviceImpl.selectVipBynumber(id);
		for(clientInf one:list) {
			 SimpleDateFormat f = new SimpleDateFormat("yyyy年MM月dd日");
			 one.setReserved1(f.format(one.getBirth()));
		}
		
		String st = JSONArray.fromObject(list).toString();
		response.getWriter().write(st);
		
	}
	
	//通过input输入内容条件查询会员信息
	@RequestMapping(value = "selectVipByother.do")
	public void selectVipByother(HttpServletRequest request,HttpServletResponse response,
			clientInf clientinf) throws IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		String clientNumber = request.getParameter("clientNumber");
		String clientfName = request.getParameter("clientfName");
		String clientId = request.getParameter("clientId");
		
		if(clientNumber != "") {
			clientinf.setNumber(clientNumber);
		}else {
			clientinf.setNumber(null);
		}
		if(clientfName != "") {
			clientinf.setName(clientfName);
		}else {
			clientinf.setName(null);
		}
		if(clientId != "") {
			clientinf.setIdnumber(clientId);
		}else {
			clientinf.setIdnumber(null);
		}
		
		List<clientInf> list = VipserviceImpl.selectVipByother(clientinf);
		
		String st = JSONArray.fromObject(list).toString();
		response.getWriter().write(st);
		
	}
	
	//楼层、房型下拉框显示
	@RequestMapping(value = "/nonmemberCheckin.do")
	public String nonmemberCheckin(HttpServletRequest request,HttpServletResponse response,
			Model model) throws UnsupportedEncodingException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		List<floor> floorlist = floorserviceImpl.queryFloorAll();
		List<houseType> houselist = roomtypeServiceImpl.queryHouseAll();
		
		model.addAttribute("floorlist",floorlist);
		model.addAttribute("houselist",houselist);
		
		return "view/checkin/checkinMain";
	}
	
	//非会员入住办理
	@RequestMapping(value = "nonmemberCheckin2.do")
	public void nonmemberCheckin2(HttpServletRequest request,HttpServletResponse response,
			kaifanRecord kaifanrecord,HttpSession session,room rom) throws IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		PrintWriter pw = response.getWriter();
		
		//参数获取
		String name = request.getParameter("name");
		String Idnumber = request.getParameter("Idnumber");
		String Address = request.getParameter("Address");
		String people = request.getParameter("people");
		String roomNumber = request.getParameter("roomNumber");
		String remarks = request.getParameter("remarks");
		
		room ro = roomserviceImpl.selectRoomByNumber2(roomNumber);
		//获取session值
		staffInf us = (staffInf) session.getAttribute("loginUser");
		
		String orderNumber = PrimaryKeyUtil.getPrimaryKey();
		
		//装箱
		kaifanrecord.setId(orderNumber);
		kaifanrecord.setName(name);
		kaifanrecord.setNumber("非会员");
		kaifanrecord.setType(ro.getType());
		kaifanrecord.setPrice(ro.getPrice());
		kaifanrecord.setQuantity(Integer.parseInt(people));
		kaifanrecord.setIdnumber(Idnumber);
		kaifanrecord.setAddress(Address);
		kaifanrecord.setTransactor(us.getName());
		kaifanrecord.setStatime(new Date());
		kaifanrecord.setRemarks(remarks);
		kaifanrecord.setRoomnumber(Integer.parseInt(roomNumber));
		//插入开房记录
		int i = KaifanRecordserviceImpl.insertSelective(kaifanrecord);
		
		
		//更新房间状态
		rom.setReserved1(orderNumber);
		rom.setStatus("使用中");
		rom.setReserved2("非会员");
		rom.setRoomnum(Integer.parseInt(roomNumber));
		rom.setReserved3(people);
		int k = roomserviceImpl.updeRoomByroomnumber(rom);
		
		if(i > 0 && k > 0) {
			pw.write("1");
		}else {
			pw.write("0");
		}
	}
	
	//获取会员信息---ajax
	@RequestMapping(value = "checkoutInf.do")
	public void checkoutInf(HttpServletRequest request,HttpServletResponse response,
			kaifanRecord kaifanrecord) throws IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		String vipNumber = request.getParameter("vipnumber");
		
		List<clientInf> list = VipserviceImpl.selectVipBynumber(vipNumber);
		
		String st = JSONArray.fromObject(list).toString();
		response.getWriter().write(st);
		
	}
	
	//退房办理
	@RequestMapping(value = "checkOut.do")
	public void checkOut(HttpServletRequest request,HttpServletResponse response,HttpSession session,
			kaifanRecord kaifanrecord,room room) throws IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		String roomNumber = request.getParameter("roomNumber");
		
		
		PrintWriter pw = response.getWriter();
		
		staffInf us = (staffInf) session.getAttribute("loginUser");
		
		room rom = roomserviceImpl.selectRoomByNumber2(roomNumber);
		
		room.setStatus("脏房");
		room.setReserved3(null);
		room.setReserved2(null);
		room.setRoomnum(Integer.parseInt(roomNumber));
		
		int j = roomserviceImpl.updeRoomByroomnumber(room);
		
		String orderNumber = rom.getReserved1();
		
		kaifanrecord.setEndtime(new Date());
		kaifanrecord.setId(orderNumber);
		kaifanrecord.setReserved1(us.getName());
		
		if(j > 0 ) {
			int i = KaifanRecordserviceImpl.updatekaifanRecordById(kaifanrecord);
			if(i > 0) {
				pw.write("1");
			}else {
				pw.write("0");
			}
			
		}else {
			pw.write("0");
		}
			
	}
	
	
	//获取非会员信息
	@RequestMapping(value = "nonmemberInf.do")
	public void nonmemberInf(HttpServletRequest request,HttpServletResponse response,HttpSession session,
			kaifanRecord kaifanrecord,room room) throws IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		String orderNumber = request.getParameter("orderNumber");
		
		kaifanRecord kaifan = KaifanRecordserviceImpl.selectkaifanRecordById(orderNumber);
		
		ArrayList<kaifanRecord> list = new ArrayList<kaifanRecord>();
		
		list.add(kaifan);
		
		String st = JSONArray.fromObject(list).toString();
		response.getWriter().write(st);
	}

}





